//
//  XMAlertView.m
//  MyNewProject
//
//  Created by apple on 2019/2/16.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "XMAlertView.h"

@implementation XMAlertView
- (id)initWithAlertViewType:(NSInteger)type{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT);
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.blackBackView];
        [self addSubview:self.whiteBackView];
        
        [self.whiteBackView addSubview:self.closedButton];
        if (type == 1) {
            self.whiteBackView.frame = CGRectMake((SCREEN_WIDTH - 260)/2.0, (SCREENH_HEIGHT - 280)/2.0, 260, 280);
            self.closedButton.frame = CGRectMake(self.whiteBackView.width - 50, 10, 40, 40);
            [self.closedButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
            
            UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.whiteBackView.width, 30)];
            tipLab.textAlignment = NSTextAlignmentCenter;
            tipLab.textColor = SKColorWithHex(0x7ab801);
            tipLab.text = @"新人须知";
            tipLab.font = BoldFont(25);
            [self.whiteBackView addSubview:tipLab];
            
            UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 140, self.whiteBackView.width, 50)];
            contentLab.numberOfLines = 0;
            contentLab.textAlignment = NSTextAlignmentCenter;
            contentLab.text = @"新用户需在捡漏专区进行十次拍卖\n熟悉拍卖规则后方可进入其他会场";
            contentLab.font = LightFont(14);
            [self.whiteBackView addSubview:contentLab];
            
            UIButton * Iknow = [UIButton buttonWithType:UIButtonTypeCustom];
            Iknow.backgroundColor = SKColorWithHex(0xE33030);
            [Iknow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [Iknow setTitle:@"我知道了" forState:UIControlStateNormal];
            Iknow.frame = CGRectMake(60, 220, 140, 40);
            Iknow.clipsToBounds = YES;
            [Iknow addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside];
            Iknow.layer.cornerRadius = 20;
            [self.whiteBackView addSubview:Iknow];
            
        }else if (type == 2){
            self.whiteBackView.frame = CGRectMake((SCREEN_WIDTH - 300)/2.0, (SCREENH_HEIGHT - 320)/2.0, 300, 320);
            self.closedButton.frame = CGRectMake(20, self.whiteBackView.height - 50, self.whiteBackView.width - 40, 40);
            [self.closedButton setTitle:@"我再看看" forState:UIControlStateNormal];
            [self.closedButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            
            UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.whiteBackView.width, 30)];
            tipLab.textAlignment = NSTextAlignmentCenter;
            tipLab.textColor = [UIColor darkTextColor];
            tipLab.text = @"您的账户保证金不足";
            tipLab.font = BoldFont(20);
            [self.whiteBackView addSubview:tipLab];
            
            UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, self.whiteBackView.width, 50)];
            contentLab.textAlignment = NSTextAlignmentCenter;
            contentLab.text = @"请先充值余额再进行操作";
            contentLab.font = LightFont(14);
            [self.whiteBackView addSubview:contentLab];
            
            UIButton * Iknow = [UIButton buttonWithType:UIButtonTypeCustom];
            Iknow.backgroundColor = SKColorWithHex(0xE33030);
            [Iknow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [Iknow setTitle:@"现在就去" forState:UIControlStateNormal];
            Iknow.frame = CGRectMake(20, self.whiteBackView.height - 100, self.whiteBackView.width - 40, 40);
            [Iknow addTarget:self action:@selector(chargePress) forControlEvents:UIControlEventTouchUpInside];
            [self.whiteBackView addSubview:Iknow];
        }
        [self addSubview:self.tipImageView];
        _tipImageView.image = [UIImage imageNamed:type == 1?@"laba":@"yuebuzu"];
    }
    return self;
}

///  弹框
/// @param msg msg
- (instancetype)initWithAlertViewWithMsg:(NSString *)msg {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT);
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.blackBackView];
        [self addSubview:self.whiteBackView];
        
        [self.whiteBackView addSubview:self.closedButton];
        self.whiteBackView.frame = CGRectMake((SCREEN_WIDTH - 300)/2.0, (SCREENH_HEIGHT - 320)/2.0, 300, 320);
        self.closedButton.frame = CGRectMake(20, self.whiteBackView.height - 50, self.whiteBackView.width - 40, 40);
        [self.closedButton setTitle:@"我再看看" forState:UIControlStateNormal];
        [self.closedButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.whiteBackView.width, 30)];
        tipLab.textAlignment = NSTextAlignmentCenter;
        tipLab.textColor = [UIColor darkTextColor];
        tipLab.text = msg;
        tipLab.font = BoldFont(20);
        [self.whiteBackView addSubview:tipLab];
        
        UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, self.whiteBackView.width, 50)];
        contentLab.textAlignment = NSTextAlignmentCenter;
        contentLab.text = @"请先充值后再进行操作";
        contentLab.font = LightFont(14);
        [self.whiteBackView addSubview:contentLab];
        
        UIButton * Iknow = [UIButton buttonWithType:UIButtonTypeCustom];
        Iknow.backgroundColor = SKColorWithHex(0xE33030);
        [Iknow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [Iknow setTitle:@"现在就去" forState:UIControlStateNormal];
        Iknow.frame = CGRectMake(20, self.whiteBackView.height - 100, self.whiteBackView.width - 40, 40);
        [Iknow addTarget:self action:@selector(chargePress) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteBackView addSubview:Iknow];
        [self addSubview:self.tipImageView];
        _tipImageView.image = [UIImage imageNamed:@"yuebuzu"];
    }
    return self;
}

- (void)chargePress
{
    [self dismissAlert];
    if (self.leftBlock) {
        self.leftBlock();
    }
}

- (void)rightBtnClicked:(id)sender
{
    
    [self dismissAlert];
    if (self.rightBlock) {
        self.rightBlock();
    }
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}

- (void)dismissAlert
{
    [self removeFromSuperview];
}

- (UIViewController *)appRootViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

-(UIImageView *)tipImageView{
    if (!_tipImageView) {
        _tipImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 144)/2.0, self.whiteBackView.y - 40, 144, 120)];
    }
    return _tipImageView;
}
-(UIView *)blackBackView{
    if (!_blackBackView) {
        _blackBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
        _blackBackView.backgroundColor = [UIColor blackColor];
        _blackBackView.alpha = 0.6;
    }
    return _blackBackView;
}
-(UIView *)whiteBackView{
    if (!_whiteBackView) {
        _whiteBackView = [[UIView alloc]initWithFrame:CGRectZero];
        _whiteBackView.backgroundColor = [UIColor whiteColor];
        _whiteBackView.clipsToBounds = YES;
        _whiteBackView.layer.cornerRadius = 5;
    }
    return _whiteBackView;
}
-(UIButton *)closedButton{
    if (!_closedButton) {
        _closedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closedButton addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closedButton;
}
@end
